<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

/*
+--------------------------------------------------------------------------
|   This module has two functions:
|   get_session_variables: Return the session variables for the class_session functions
|   parse_online_entries: Parses an array from online.php
|   See each function for more information
+--------------------------------------------------------------------------
*/

//-----------------------------------------
// This must always be 'components_location_{component_key}'
//-----------------------------------------

class components_location_ineo
{
	var $ipsclass;
	
	/*-------------------------------------------------------------------------*/
	// get_session_variables
	// Returns:
	// array( '1_type' => {location type #1} [ char(10) ]
	//        '1_id'   => {location ID #1}   [ int(10)  ]
	//        '2_type' => {location type #2} [ char(10) ]
	//        '2_id'   => {location ID #2}   [ int(10)  ]
	//		  '3_type' => {location type #3} [ char(10) ]
	//        '3_id'   => {location ID #3}   [ int(10)  ]
	//      );
	// All are optional.
	// Use this to populate the 'module_id_*' fields in the session table
	// so you can check in your own scripts it the member is active in your module
	/*-------------------------------------------------------------------------*/
	
	function get_session_variables()
	{
	  if ( isset($this->ipsclass->input['showarticle'] ) )
	  {
	    $type = 'showart';
	    $id = $this->ipsclass->input['showarticle'];
	  }
	  elseif ( isset($this->ipsclass->input['showcat']) )
	  {
	    $type = 'showcat';
	    $id = $this->ipsclass->input['showcat'];
	  }
	  else
	  {
	    $type = 'idx';
	    $id = '';
	  }
	  
		return array( '1_type' => $type,
		              '1_id'   => $id,
                );
	}
		
	/*-------------------------------------------------------------------------*/
	// parse_online_entries
	// INPUT: $array IS:
	// $array[ $session_id ] = $session_array;
	// Session array is DB row from ibf_sessions
	// EXPECTED RETURN ------------------------------------
	// $array[ $session_id ]['_parsed'] = 1;
	// $array[ $session_id ]['_url']    = {Location url}
	// $array[ $session_id ]['_text']   = {Location text}
	// $array[ $session_id ] = $session_array...
	//
	// YOU ARE RESPONSIBLE FOR PERMISSION CHECKS. IF THE MEMBER DOESN'T
	// HAVE PERMISSION RETURN '_url'    => $this->ipsclass->base_url,
	// 						  '_text'   => $this->ipsclass->lang['board_index'],
	//						  '_parsed' => 1 { as well as the rest of $session_array }
	/*-------------------------------------------------------------------------*/
	
	function parse_online_entries( $array=array() )
	{
		$this->ipsclass->load_language( 'lang_ineo' );
		
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$return = array();
		
		//-----------------------------------------
		// LOOP
		//-----------------------------------------
		
		if ( is_array( $array ) and count( $array ) )
		{
		  foreach( $array as $session_id => $session_array )
		  {
		    if ( $session_array['location_1_type'] == 'showart' )
		    {
		      $art_ids[] = intval($session_array['location_1_id']);
		    }
		    elseif ( $session_array['location_1_type'] == 'showcat' )
		    {
		      $cat_ids[] = intval($session_array['location_1_id']);
		    }
		  }
		  
		  // Pull article titles if articles is bein' viewed.
		  if ( count($art_ids) )
		  {
		    $art_query_string = 'article_id IN('.implode( ',', $art_ids ).')';
		    
		    $this->ipsclass->DB->simple_construct( array('select' => 'article_id, article_title', 'from' => 'ineo_articles',
		                                                 'where' => $art_query_string ) );
		    $this->ipsclass->DB->simple_exec();
		    
		    while ( $row = $this->ipsclass->DB->fetch_row() )
		    {
		      $arts[$row['article_id']] = $row['article_title'];
		    }
		  }
		  
		  // Den same thing, fer cats.
		  if ( count($cat_ids) )
		  {
		    $cat_query_string = 'category_id IN('.implode( ',', $cat_ids ).')';
		    
		    $this->ipsclass->DB->simple_construct( array('select' => 'category_id,category_name', 'from' => 'ineo_categories',
		                                                 'where' => $cat_query_string ) );
		    $this->ipsclass->DB->simple_exec();
		    
		    while ( $row = $this->ipsclass->DB->fetch_row() )
		    {
		      $cats[$row['category_id']] = $row['category_name'];
		    }
		  }
		  
		  foreach( $array as $session_id => $session_array )
			{
			  if ( $session_array['location'] == 'mod:ineo' )
			  {
			    if ( $session_array['location_1_type'] == 'showart' )
			    {
			      $location = $this->ipsclass->base_url.'autocom=ineo&showarticle='.$session_array['location_1_id'];
			      $text = $this->ipsclass->lang['ineo_act_art'].' '.$arts[$session_array['location_1_id']];
			    }
			    elseif ( $session_array['location_1_type'] == 'showcat' )
			    {
			      $location = $this->ipsclass->base_url.'autocom=ineo&showcat='.$session_array['location_1_id'];
			      $text = $this->ipsclass->lang['ineo_act_cat'].' '.$cats[$session_array['location_1_id']];
			    }
			    else
			    {
			      $location = $this->ipsclass->base_url.'autocom=ineo';
			      $text = $this->ipsclass->lang['ineo_act_idx'];
			    }
			  }
				else
				{
					$location = $this->ipsclass->base_url;
					$text     = $this->ipsclass->lang['board_index'];
				}
				$return[ $session_id ] = array_merge( $session_array, array( '_url' => $location, '_text' => $text, '_parsed' => 1 ) );
			}
		}
		
		return $return;
	}
	
	

}

?>